<?php

namespace Biz\model\activity;

use Doraemon\pockets\datebase\BaseDbModel;

class ActivityRegisterInfo extends BaseDbModel
{
    const _CONFIG_FILE_ = 'db_shop';

    const _CONFIG_SELECT_ = 'db_activity';

    const _DATABASE_ = 'shop_activity';

    const _TABLE_NAME = 'activity_register_info';

    const STATUS_NORMAL = 1;
    const STATUS_CLOSE = 0;
    const STATUS_DELETE = 9;


    const ALL_SHOP_YES = 1; // 全部商家
	const ALL_SHOP_NO = 0; // 指定商家
	const ALL_SHOP_GREY_LIST = 2; // 排除灰名单商家

    const STATUS_DESC = [
        self::STATUS_NORMAL => '正常',
        self::STATUS_CLOSE => '关闭',
        self::STATUS_DELETE => '已删除',
    ];

    public static function insertData($data)
    {
        $ret = self::insert($data);
        if ($ret->errorCode() !== '00000') {
            throw new \Exception('创建报名活动失败');
        }
        return self::id();
    }

    public static function updateData($data, $where)
    {
        $ret = self::update($data, $where);
        if ($ret->errorCode() !== '00000') {
            throw new \Exception('修改报名活动失败');
        }
        return true;
    }

    public static function getRegisterInfo($registerId)
    {
        $info = self::get('*', ['id' => $registerId, 'status[!]' => self::STATUS_DELETE]);
        self::formatInfo($info);
        return $info;
    }

    public static function getRegisterInfos($where, $limit = [], $order = [])
    {
        $param = [
            'AND' => $where,
        ];
        if ($limit) {
            $param['LIMIT'] = $limit;
        }
        if ($order) {
            $param['ORDER'] = $order;
        }
        $list = self::select('*', $param);
        $result = [];
        foreach ($list as $info) {
            self::formatInfo($info);
            $result[$info['id']] = $info;
        }
        return $result;
    }

    public static function getRegisterInfosCount($where)
    {
        return self::count('*', $where);
    }

    public static function getRegisterInfoByRegisterId($registerId, $field = '*')
    {
        $info = self::get($field, ['id' => $registerId]);
        self::formatInfo($info);
        return $info;
    }

    public static function getInfosByRegisterIds($registerIds)
	{
		$results = self::select('*', [
			'id' => $registerIds,
			'status' => self::STATUS_NORMAL
		]);

		return array_column($results, null, 'id');
	}

    private static function formatInfo(&$info)
    {
        foreach ($info as $key => $value) {
            switch ($key) {
                case 'status':
                    $info['status_desc'] = self::STATUS_DESC[$value];
            }
        }
    }
}